---@class RedRob.Skill:RedRob.SkillBase
---@field New fun():RedRob.Skill
local Skill = RedRob.CreateClass("Skill","SkillBase")
---@param actor RedRob.Player
function Skill:OnCreate(actor,skillId)
    RedRob.SkillBase.OnCreate(self,actor:GetId(),skillId)
    ---@type RedRob.Player
    self.actor = actor
    ---@type RedRob.SkillNode[]
    self.nodeList = {}
    self.isHit = false
    self.skillCfg = dataTable.getDataTableByStringId("t_skill",skillId)
    if not self.skillCfg then
        logError("找不到技能t_skill "..skillId)
        return
    end
    self.showCfg = dataTable.getDataTableByStringId("t_skillshow",skillId)
    if not self.showCfg then
        logError("找不到技能t_skillshow "..skillId)
        return
    end
    self.maxCd = self.skillCfg.f_GuideTime/1000
    self:InitNode()
    self.isNextSkillStartCoolDown = false
    self.nextSkillId = 0
    self.nextSkillDelayTime = 0
    self.nextSkillCurTime = 0
    if self.skillCfg.f_SkillBySkill ~= "" then
        local delay = self.skillCfg.f_SkillBySkill[1][1]
        local id = self.skillCfg.f_SkillBySkill[1][2]
        self.nextSkillDelayTime = delay/1000
        self.actor.skillMgr:CreateSkill(id)
        self.nextSkillId = id
    end
    self.damageData = {value = 200}
end
function Skill:OnDestroy()
    if self.nextSkillId > 0 then
        local s = self.actor.skillMgr:GetSkill(self.nextSkill)
        if s then
            s:OnDestroy()
        end
    end
    self.nextSkillId = 0
    self.actor = nil
end
function Skill:InitNode()
    local NODE_TYPE = RedRob.Config.SkillNodeType
    local p = self.showCfg.f_SkillFPosition
    local res = self.showCfg.f_SkillFResource
    local action = self.showCfg.f_SkillFName
    local node
    if not string.IsNilOrEmpty(res) then    --前特效
        if type(res) == "string" then
            res = string.split(res,",")
        end
        for k,v in pairs(res) do
            node = RedRob.SkillNode.CreateNode(self,NODE_TYPE.effect,action,v,p[1],p[2],p[3])
            self:AddNode(node)
        end
    end
    p = self.showCfg.f_SkillBPosition
    res = self.showCfg.f_SkillBResource
    action = self.showCfg.f_SkillBName
    if not string.IsNilOrEmpty(res) then    --后特效
        if type(res) == "string" then
            res = string.split(res,",")
        end
        for k,v in pairs(res) do
            node = RedRob.SkillNode.CreateNode(self,NODE_TYPE.effect,action,v,p[1],p[2],p[3])
            self:AddNode(node)
        end
    end

    action = self.showCfg.f_AnimResource
    if not string.IsNilOrEmpty(action) then    --动作
        node = RedRob.SkillNode.CreateNode(self,NODE_TYPE.action,action)
        self:AddNode(node)
    end

    p = self.showCfg.f_FlyFPosition
    res = self.showCfg.f_FlyFResource
    action = self.showCfg.f_FlyFName
    if not string.IsNilOrEmpty(res) then    --飞行道具
        if type(res) == "string" then
            res = string.split(res,",")
        end
        for k,v in pairs(res) do
            node = RedRob.SkillNode.CreateNode(self,NODE_TYPE.bullet,action, v,p[1],p[2],p[3])
            self:AddNode(node)
        end
    end
end
---@param node RedRob.SkillNode
function Skill:AddNode(node)
    table.insert(self.nodeList,node)
end

function Skill:SetDamageValue(damageValue)
    self.damageData.value = damageValue
    if self.nextSkillId > 0 then
        local skill = self.actor.skillMgr:GetSkill(self.nextSkillId)
        skill:SetDamageValue(damageValue)
    end
end
function Skill:DoSkill()
    if self:IsInCD() then
        return false
    end
    self:OnSkillInit()
    self:OnSkillStart()
    self:StartCoolDown(self.maxCd)
    self:OnSkillSpell()
    return true
end

function Skill:EndSkill(excludeNextSkill)
    self.isInCd = false
    self.isEnd = true
    for k,v in pairs(self.nodeList) do
        v:SkillEnd()
    end
    if self.nextSkillId > 0 and not excludeNextSkill then
        self.actor.skillMgr:EndSkill(self.nextSkillId)
    end
    self:OnSkillBackSwing()
    self:OnSkillEnd()
end
function Skill:Update(dt)
    if self.isEnd then return end
    if self.isNextSkillStartCoolDown then
        self.nextSkillCurTime = self.nextSkillCurTime + dt
        if self.nextSkillCurTime >= self.nextSkillDelayTime then
            self.isNextSkillStartCoolDown = false
            self.actor.skillMgr:DoSkill(self.nextSkillId)
        end
    end
    for k,v in pairs(self.nodeList) do
        v:Update(dt)
    end
    RedRob.SkillBase.Update(self,dt)
end

function Skill:DoDamage()
    local target = self.actor:GetTarget()
    if target and not target:IsDead() and self.damageData.value > 0 then
        target:Hurt(self.damageData.value,self.actor)
    end
end
function Skill:Hit()
    if self.isHit then
        return
    end
    self.isHit = true
    local x,y = self:GetHitPos()
    if x == nil then
        return
    end
    local target = self.actor:GetTarget()
    local targetId = target == nil and 0 or target:GetId()
    self.actor:Print("Skill:Hit ","targetId:"..targetId,"damage:"..self.damageData.value)
    local resName = "Prefabs/Skill/"..self.showCfg.f_HitFResource
    local action = self.showCfg.f_HitFName
    local order = 0
    if target then
        order = target:GetSortingOrder()
    else
        order = self.actor:GetSortingOrder()
    end
    RedRob.EffectMgr.CreateEffect(self.actor:GetId(),resName,action,x,y,nil,order)
    self:DoDamage()
    if not string.IsNilOrEmpty(self.showCfg.f_HitAudio) then
        AudioManager.Play(self.showCfg.f_HitAudio)
    end
end
---pos
function Skill:GetHitPos()
    local target = self.actor:GetTarget()
    if not target then return 0,0 end
    local hitPos = self.showCfg.f_HitFPosition
    local offX = tonumber(hitPos[1])
    local offY = tonumber(hitPos[2])
    local pos = target:GetPos()
    if target.isLeft then
        offX = -offX
    end
    if target.actorType ~= RedRob.Config.ActorType.player then
        return pos.x,pos.y
    end
    if hitPos[3] == nil then
        return pos.x + offX,pos.y + offY
    end
    local x,y = target:GetBonePos(hitPos[3])
    return x + offX,y + offY
end
---CD倒计时结束
function Skill:CoolDownFinished()
    self.actor.skillMgr:EndSkill(self.skillId)
end
---技能初始化
function Skill:OnSkillInit()
    RedRob.SkillBase.OnSkillInit(self)
    self.actor:Print("DoSkill",self.skillId)
    self.isHit = false
end
---技能起手(前揺)
function Skill:OnSkillStart()
    for k,v in pairs(self.nodeList) do
        v:SkillStart()
    end
    if not string.IsNilOrEmpty(self.showCfg.f_SkillAudio) then
        AudioManager.Play(self.showCfg.f_SkillAudio)
    end
    if self.nextSkillId > 0 then
        self.isNextSkillStartCoolDown = true
        self.nextSkillCurTime = 0
    end
end
---技能施法
function Skill:OnSkillSpell()

end

---技能后摇
function Skill:OnSkillBackSwing()

end
---技能结束
function Skill:OnSkillEnd()
    RedRob.SkillBase.OnSkillEnd(self)
end